extends "res://BaseEnemy.gd"

# 沙漠蝎子特有属性
@export var poison_damage = 3
@export var poison_duration = 5.0
@export var poison_chance = 0.3  # 造成中毒的几率

# 中毒状态追踪
var poisoned_targets = {}

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置沙漠蝎子特有属性
	max_health = 35
	current_health = max_health
	movement_speed = 90.0
	damage = 7
	experience_value = 6
	attack_range = 25.0
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.8, 0.7, 0.2)  # 沙漠蝎子颜色

func _process(delta):
	# 调用父类的_process方法
	super._process(delta)
	
	# 处理中毒效果
	var targets_to_remove = []
	for target in poisoned_targets.keys():
		if is_instance_valid(target):
			poisoned_targets[target].time -= delta
			poisoned_targets[target].tick_timer += delta
			
			# 每秒造成一次中毒伤害
			if poisoned_targets[target].tick_timer >= 1.0:
				poisoned_targets[target].tick_timer = 0.0
				if target.has_method("take_damage"):
					target.take_damage(poison_damage)
					print("中毒伤害: ", poison_damage)
			
			# 中毒效果结束
			if poisoned_targets[target].time <= 0:
				targets_to_remove.append(target)
		else:
			targets_to_remove.append(target)
	
	# 移除已结束中毒效果的目标
	for target in targets_to_remove:
		poisoned_targets.erase(target)

func attack_player():
	if animation_player and not animation_player.is_playing():
		if animation_player.has_animation("sting"):
			animation_player.play("sting")
			
			# 对玩家造成伤害
			if target_player and target_player.has_method("take_damage"):
				target_player.take_damage(damage)
				
				# 尝试造成中毒效果
				if randf() < poison_chance:
					apply_poison(target_player)
		else:
			# 如果没有特殊攻击动画，使用普通攻击
			super.attack_player()

func apply_poison(target):
	# 如果目标已经中毒，刷新持续时间
	if poisoned_targets.has(target):
		poisoned_targets[target].time = poison_duration
	else:
		# 否则添加新的中毒效果
		poisoned_targets[target] = {
			"time": poison_duration,
			"tick_timer": 0.0
		}
	
	print("目标中毒!")
	
	# 视觉效果（如果目标有sprite属性）
	if target.has_method("apply_poison_effect"):
		target.apply_poison_effect(poison_duration)
	elif target.has_node("Sprite2D"):
		var target_sprite = target.get_node("Sprite2D")
		target_sprite.modulate = Color(0.7, 1.0, 0.7)  # 绿色调
		var tween = create_tween()
		tween.tween_property(target_sprite, "modulate", Color(1, 1, 1), 0.5)
